Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.3     v dplyr   1.0.7
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   2.0.0     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./year1.RDS")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 364)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 13.05403 13.04925 13.04454 13.03990 13.03533 13.03083 13.02639 13.02201
##   [9] 13.01768 13.01342 13.00921 13.00505 13.00094 12.99688 12.99287 12.98890
##  [17] 12.98497 12.98108 12.97722 12.97340 12.96962 12.96586 12.96214 12.95843
##  [25] 12.95476 12.95110 12.94747 12.94385 12.94025 12.93666 12.93308 12.92951
##  [33] 12.92594 12.92238 12.91883 12.91527 12.91171 12.90816 12.90462 12.90110
##  [41] 12.89759 12.89410 12.89063 12.88719 12.88378 12.88040 12.87706 12.87376
##  [49] 12.87049 12.86728 12.86411 12.86099 12.85793 12.85492 12.85198 12.84910
##  [57] 12.84629 12.84355 12.84088 12.83829 12.83578 12.83335 12.83101 12.82876
##  [65] 12.82661 12.82455 12.82258 12.82073 12.81897 12.81733 12.81580 12.81431
##  [73] 12.81280 12.81127 12.80974 12.80820 12.80666 12.80513 12.80361 12.80210
##  [81] 12.80062 12.79916 12.79774 12.79636 12.79502 12.79373 12.79249 12.79131
##  [89] 12.79020 12.78916 12.78819 12.78731 12.78651 12.78580 12.78519 12.78469
##  [97] 12.78429 12.78401 12.78384 12.78380 12.78389 12.78412 12.78448 12.78499
## [105] 12.78565 12.78647 12.78739 12.78833 12.78931 12.79033 12.79139 12.79249
## [113] 12.79363 12.79482 12.79607 12.79737 12.79873 12.80015 12.80163 12.80317
## [121] 12.80479 12.80648 12.80824 12.81008 12.81200 12.81400 12.81609 12.81826
## [129] 12.82053 12.82290 12.82536 12.82792 12.83059 12.83336 12.83624 12.84018
## [137] 12.84600 12.85350 12.86251 12.87282 12.88427 12.89664 12.90977 12.92345
## [145] 12.93751 12.95175 12.96598 12.98001 12.99367 13.00675 13.01908 13.03046
## [153] 13.04070 13.04962 13.05702 13.06273 13.06882 13.07733 13.08795 13.10039
## [161] 13.11435 13.12955 13.14567 13.16242 13.17952 13.19666 13.21354 13.22988
## [169] 13.24537 13.25972 13.27263 13.28381 13.29295 13.29978 13.30398 13.30691
## [177] 13.31008 13.31345 13.31696 13.32058 13.32424 13.32791 13.33153 13.33506
## [185] 13.33845 13.34164 13.34460 13.34728 13.34962 13.35158 13.35311 13.35416
## [193] 13.35468 13.35463 13.35396 13.35262 13.35056 13.34773 13.34409 13.33958
## [201] 13.33417 13.32779 13.32041 13.31197 13.30156 13.28852 13.27318 13.25588
## [209] 13.23696 13.21674 13.19556 13.17377 13.15168 13.12965 13.10801 13.08708
## [217] 13.06722 13.04874 13.03199 13.01445 12.99361 12.96980 12.94336 12.91462
## [225] 12.88391 12.85157 12.81794 12.78334 12.74812 12.71261 12.67715 12.64206
## [233] 12.60768 12.57436 12.54242 12.51219 12.48402 12.45823 12.43517 12.41516
## [241] 12.39641 12.37695 12.35689 12.33634 12.31541 12.29419 12.27280 12.25135
## [249] 12.22993 12.20865 12.18763 12.16697 12.14676 12.12713 12.10818 12.09000
## [257] 12.07272 12.05643 12.04124 12.02778 12.01644 12.00695 11.99907 11.99252
## [265] 11.98704 11.98238 11.97827 11.97445 11.97067 11.96665 11.96214 11.95689
## [273] 11.95062 11.94308 11.93400 11.92476 11.91682 11.91002 11.90420 11.89923
## [281] 11.89495 11.89121 11.88787 11.88477 11.88175 11.87869 11.87541 11.87178
## [289] 11.86764 11.86285 11.85725 11.85070 11.84304 11.83413 11.82476 11.81580
## [297] 11.80719 11.79883 11.79068 11.78265 11.77468 11.76669 11.75862 11.75039
## [305] 11.74194 11.73320 11.72408 11.71454 11.70448 11.69385 11.68298 11.67222
## [313] 11.66156 11.65096 11.64040 11.62983 11.61925 11.60861 11.59789 11.58705
## [321] 11.57608 11.56494 11.55360 11.54203 11.53021 11.51810 11.50568 11.49292
## [329] 11.47979 11.46640 11.45290 11.43927 11.42552 11.41163 11.39761 11.38344
## [337] 11.36913 11.35466 11.34003 11.32524 11.31028 11.29515 11.27984 11.26434
## [345] 11.24866 11.23273 11.21653 11.20005 11.18332 11.16635 11.14914 11.13172
## [353] 11.11408 11.09624 11.07823 11.06003 11.04168 11.02318 11.00453 10.98577
## [361] 10.96689 10.94790 10.92883 10.90967
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year1.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.6, n = 364)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.62499 12.62034 12.61579 12.61133 12.60696 12.60269 12.59851 12.59442
##   [9] 12.59041 12.58650 12.58267 12.57894 12.57528 12.57172 12.56823 12.56483
##  [17] 12.56152 12.55828 12.55513 12.55206 12.54906 12.54615 12.54331 12.54055
##  [25] 12.53786 12.53525 12.53271 12.53025 12.52785 12.52553 12.52328 12.52110
##  [33] 12.51899 12.51695 12.51497 12.51306 12.51121 12.50942 12.50770 12.50604
##  [41] 12.50445 12.50293 12.50148 12.50010 12.49880 12.49757 12.49642 12.49534
##  [49] 12.49435 12.49344 12.49261 12.49187 12.49122 12.49065 12.49018 12.48980
##  [57] 12.48951 12.48932 12.48923 12.48924 12.48935 12.48956 12.48987 12.49029
##  [65] 12.49082 12.49146 12.49221 12.49307 12.49405 12.49515 12.49636 12.49773
##  [73] 12.49930 12.50106 12.50300 12.50513 12.50744 12.50993 12.51258 12.51540
##  [81] 12.51837 12.52151 12.52480 12.52823 12.53181 12.53553 12.53938 12.54337
##  [89] 12.54748 12.55171 12.55606 12.56052 12.56509 12.56977 12.57455 12.57942
##  [97] 12.58438 12.58943 12.59456 12.59977 12.60505 12.61041 12.61582 12.62130
## [105] 12.62684 12.63242 12.63806 12.64373 12.64945 12.65520 12.66098 12.66679
## [113] 12.67262 12.67846 12.68432 12.69018 12.69605 12.70192 12.70779 12.71364
## [121] 12.71948 12.72530 12.73111 12.73688 12.74262 12.74833 12.75400 12.75962
## [129] 12.76520 12.77072 12.77618 12.78159 12.78693 12.79289 12.80008 12.80838
## [137] 12.81764 12.82776 12.83858 12.84999 12.86186 12.87405 12.88644 12.89890
## [145] 12.91130 12.92350 12.93539 12.94683 12.95768 12.96783 12.97715 12.98549
## [153] 12.99274 12.99877 13.00614 13.01715 13.03128 13.04800 13.06677 13.08708
## [161] 13.10839 13.13017 13.15190 13.17305 13.19309 13.21149 13.22771 13.24125
## [169] 13.25156 13.25811 13.26226 13.26571 13.26848 13.27057 13.27200 13.27278
## [177] 13.27291 13.27243 13.27132 13.26962 13.26732 13.26445 13.26100 13.25701
## [185] 13.25246 13.24739 13.24180 13.23570 13.22911 13.22203 13.21448 13.20648
## [193] 13.19802 13.18913 13.17982 13.17009 13.15996 13.14945 13.13856 13.12731
## [201] 13.11570 13.10376 13.09148 13.07889 13.06600 13.05281 13.03935 13.02561
## [209] 13.01162 12.99739 12.98292 12.96823 12.95333 12.93824 12.92296 12.90751
## [217] 12.89190 12.87469 12.85464 12.83204 12.80721 12.78043 12.75201 12.72226
## [225] 12.69146 12.65993 12.62796 12.59586 12.56392 12.53245 12.50174 12.47210
## [233] 12.44383 12.41723 12.39259 12.37023 12.35044 12.33351 12.31782 12.30157
## [241] 12.28489 12.26788 12.25067 12.23337 12.21609 12.19895 12.18206 12.16554
## [249] 12.14950 12.13406 12.11932 12.10542 12.09245 12.08054 12.06986 12.06047
## [257] 12.05231 12.04530 12.03936 12.03444 12.03045 12.02732 12.02500 12.02339
## [265] 12.02244 12.02207 12.02222 12.02280 12.02376 12.02501 12.02649 12.02812
## [273] 12.02984 12.03158 12.03326 12.03481 12.03616 12.03724 12.03799 12.03832
## [281] 12.03817 12.03746 12.03613 12.03411 12.03132 12.02769 12.02315 12.01763
## [289] 12.01107 12.00399 11.99699 11.99006 11.98317 11.97632 11.96950 11.96268
## [297] 11.95587 11.94905 11.94220 11.93532 11.92838 11.92139 11.91432 11.90716
## [305] 11.89990 11.89254 11.88505 11.87742 11.86964 11.86171 11.85375 11.84592
## [313] 11.83818 11.83051 11.82290 11.81531 11.80773 11.80013 11.79249 11.78479
## [321] 11.77701 11.76911 11.76109 11.75291 11.74455 11.73600 11.72722 11.71820
## [329] 11.70892 11.69943 11.68983 11.68011 11.67027 11.66032 11.65025 11.64008
## [337] 11.62979 11.61939 11.60888 11.59827 11.58754 11.57672 11.56578 11.55475
## [345] 11.54361 11.53236 11.52102 11.50958 11.49804 11.48641 11.47468 11.46285
## [353] 11.45093 11.43892 11.42681 11.41462 11.40233 11.38996 11.37750 11.36495
## [361] 11.35232 11.33961 11.32681 11.31393
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year1.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.6, n = 364)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 12.05956 12.05278 12.04611 12.03955 12.03310 12.02676 12.02052 12.01439
##   [9] 12.00836 12.00242 11.99659 11.99085 11.98520 11.97965 11.97418 11.96880
##  [17] 11.96350 11.95829 11.95316 11.94810 11.94313 11.93823 11.93340 11.92864
##  [25] 11.92395 11.91932 11.91477 11.91027 11.90583 11.90146 11.89713 11.89287
##  [33] 11.88865 11.88449 11.88037 11.87630 11.87228 11.86829 11.86435 11.86044
##  [41] 11.85657 11.85274 11.84893 11.84518 11.84149 11.83787 11.83432 11.83085
##  [49] 11.82745 11.82413 11.82090 11.81775 11.81468 11.81170 11.80881 11.80602
##  [57] 11.80332 11.80072 11.79822 11.79582 11.79353 11.79134 11.78927 11.78730
##  [65] 11.78545 11.78372 11.78210 11.78061 11.77924 11.77800 11.77688 11.77590
##  [73] 11.77505 11.77434 11.77376 11.77332 11.77303 11.77288 11.77274 11.77247
##  [81] 11.77210 11.77163 11.77108 11.77046 11.76980 11.76910 11.76838 11.76765
##  [89] 11.76693 11.76624 11.76558 11.76498 11.76445 11.76400 11.76364 11.76340
##  [97] 11.76329 11.76332 11.76350 11.76386 11.76440 11.76515 11.76611 11.76730
## [105] 11.76874 11.77044 11.77241 11.77468 11.77724 11.78013 11.78335 11.78693
## [113] 11.79086 11.79528 11.80025 11.80576 11.81175 11.81820 11.82507 11.83233
## [121] 11.83993 11.84785 11.85604 11.86448 11.87312 11.88194 11.89089 11.89994
## [129] 11.90906 11.91821 11.92735 11.93644 11.94546 11.95437 11.96313 11.97331
## [137] 11.98632 12.00186 12.01968 12.03949 12.06100 12.08396 12.10806 12.13305
## [145] 12.15863 12.18453 12.21048 12.23619 12.26139 12.28580 12.30913 12.33112
## [153] 12.35149 12.36994 12.38622 12.40004 12.41462 12.43304 12.45483 12.47952
## [161] 12.50663 12.53569 12.56622 12.59775 12.62981 12.66191 12.69360 12.72438
## [169] 12.75379 12.78136 12.80660 12.82905 12.84823 12.86366 12.87488 12.88367
## [177] 12.89214 12.90026 12.90801 12.91536 12.92229 12.92877 12.93479 12.94031
## [185] 12.94532 12.94978 12.95368 12.95698 12.95968 12.96173 12.96312 12.96383
## [193] 12.96382 12.96308 12.96158 12.95929 12.95620 12.95228 12.94750 12.94184
## [201] 12.93527 12.92778 12.91933 12.90991 12.89711 12.87903 12.85644 12.83008
## [209] 12.80071 12.76906 12.73590 12.70198 12.66803 12.63482 12.60310 12.57361
## [217] 12.54710 12.52433 12.50604 12.48864 12.46822 12.44509 12.41957 12.39196
## [225] 12.36259 12.33175 12.29977 12.26695 12.23361 12.20006 12.16661 12.13356
## [233] 12.10124 12.06996 12.04002 12.01174 11.98543 11.96140 11.93996 11.92143
## [241] 11.90454 11.88781 11.87125 11.85486 11.83864 11.82260 11.80673 11.79104
## [249] 11.77553 11.76020 11.74506 11.73010 11.71534 11.70076 11.68638 11.67219
## [257] 11.65820 11.64441 11.63082 11.61824 11.60733 11.59788 11.58969 11.58254
## [265] 11.57623 11.57055 11.56529 11.56025 11.55521 11.54997 11.54432 11.53805
## [273] 11.53096 11.52283 11.51345 11.50361 11.49420 11.48518 11.47652 11.46818
## [281] 11.46012 11.45232 11.44472 11.43731 11.43003 11.42286 11.41576 11.40870
## [289] 11.40163 11.39453 11.38735 11.38007 11.37263 11.36502 11.35742 11.35002
## [297] 11.34281 11.33578 11.32891 11.32217 11.31557 11.30907 11.30266 11.29632
## [305] 11.29005 11.28381 11.27760 11.27140 11.26519 11.25896 11.25281 11.24687
## [313] 11.24112 11.23554 11.23012 11.22483 11.21966 11.21459 11.20961 11.20469
## [321] 11.19983 11.19499 11.19017 11.18535 11.18051 11.17563 11.17070 11.16569
## [329] 11.16060 11.15546 11.15036 11.14528 11.14023 11.13522 11.13025 11.12533
## [337] 11.12045 11.11562 11.11085 11.10613 11.10148 11.09689 11.09237 11.08792
## [345] 11.08355 11.07925 11.07504 11.07089 11.06682 11.06283 11.05891 11.05506
## [353] 11.05129 11.04759 11.04396 11.04041 11.03692 11.03351 11.03017 11.02691
## [361] 11.02371 11.02058 11.01753 11.01454
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year1.rda")

keeping in case

#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")